﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Common.Tools.Converter
{
    /// <summary>
    /// IconFont在Xaml中的转换器
    /// <para>
    ///     由于在Xaml中使用IconFont的字符串，会出现空格的问题，需要在使用时转义字符串
    /// </para>
    /// <para>
    ///     使用方式 Text={Binding IconFont,Converter={StaticResource IconFontConverter}}
    /// </para>
    /// </summary>
    public class IconFontConvert: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                string strFont = value.ToString();
                strFont = Regex.Unescape(strFont.Replace("&#x", @"\u").Replace(";", ""));// &#xe74f;
                return strFont;
            }
            else
            {
                return value;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}
